/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChromosomeRecord.java
 *
 * Created on 27/Dez/2011, 22:35:20
 */

package intopo.evogame.partnerselection.tools.analyser;

import intopo.game.ReadStrategy;
import intopo.game.Strategy;

import java.io.DataInput;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 27/Dez/2011
 *
 * @param <S>
 */
class ChromosomeRecord<S extends Strategy<S>>
	extends Record
{
	byte poolSize;
	/**
	 * Utility threshold used in the decision to update the probability and
	 * combination vectors.  This field represents parameter
	 * <i>u<sub>t</sub></i> mentioned in the paper.
	 *
	 * <p><b>NOTE:</b> In this implementation, utility is equal to the
	 * payoff obtained by the player in the game.
	 */
	float utilityThreshold;
	/**
	 * Multiplication factor used when a probability is decreased because
	 * the player obtained a utility lower than {@code utilityThreshold}.
	 * This field represents parameter <i>&delta;</i> mentioned in the
	 * paper.
	 */
	float delta;
	/**
	 * Number of bits used in encoding probabilities of the probability vector.  Valid values range from 1 to 8.
	 */
	byte bitsPerProbability;
	S strategy;
	/**
	 *
	 * @param read
	 */
	ChromosomeRecord (ReadStrategy<S> read)
	{
		super (14 + read.strategySize ());
	}
	/**
	 * Read a record with the given key from the {@code chromosome} table.  Records in this table are sorted by the primary key.  Whenever a new chromosome is constructed, it is written to this table.
	 * <p> This method assumes that all records have the same strategy.  This means record size is constant.
	 * @param raf  The file where the table is stored
	 * @param id  The primary key of the record to read
	 * @param read   The object used to read a strategy.
	 * @throws java.io.IOException
	 * @see intopo.evogame.partnerselection.chromosome.Chromosome
	 * @see ReadStrategy#strategySize()
	 */
	void read (RandomAccessFile raf, int id, ReadStrategy<S> read)
		throws IOException
	{
		if (!this.position (raf, id)) {
			throw new IOException ("Could not find chromosome record with id " + id);
		}
		this.readRest (raf, read);
	}
	/**
	 * Read the next record from the chromosome table.
	 * @param di  The file where the table is stored.
	 * @param read  The object used to read a strategy.
	 * @throws java.io.IOException
	 */
	void read (DataInput di, ReadStrategy<S> read)
		throws IOException
	{
		this.ID = di.readInt ();
		this.readRest (di, read);
	}
	private void readRest (DataInput ri, ReadStrategy<S> read)
		throws IOException
	{
		this.poolSize = ri.readByte ();
		this.utilityThreshold = ri.readFloat ();
		this.delta = ri.readFloat ();
		this.bitsPerProbability = ri.readByte ();
		this.strategy = read.read (ri);
	}
	@Override
	public int hashCode ()
	{
		return this.ID;
	}
	@Override
	public boolean equals (Object other)
	{
		return this.ID == ((ChromosomeRecord<S>) other).ID;
	}
	@Override
	public String toString ()
	{
		return String.format ("ID %d  l %d  ut %f  δ %f  b %d  S %s",
			this.ID,
			this.poolSize,
			this.utilityThreshold,
			this.delta,
			this.bitsPerProbability,
			this.strategy.toString ());
	}
}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
